<div id="app">
    <section class="content-header">
        <h1>
            <small>品牌列表</small>
        </h1>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-header with-border">
                        <div class="box-tools">
                            <div class="btn-group pull-right" style="margin-right: 5px">
                                <a href="/admin/brand/add" class="btn btn-sm btn-default" title="添加品牌"> <span class="hidden-xs">添加品牌</span></a>
                            </div>
                        </div>

                        <span>


    <a class="btn btn-sm btn-primary grid-refresh" title="刷新"><i class="fa fa-refresh"></i><span class="hidden-xs"> 刷新</span></a>

    <div class="btn-group" style="margin-right: 10px" data-toggle="buttons">
        <label class="btn btn-sm btn-dropbox shaixuan-filter-btn" title="筛选">
            <input type="checkbox"><i class="fa fa-filter"></i><span class="hidden-xs">筛选</span>
        </label>
    </div>
  </span>
                    </div>

                    <!--   筛选开始   -->
                    <div class="box-header with-border hide" id="filter-box">
                        <form action="/admin/brand/index" class="form-horizontal" pjax-container method="get">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="box-body">
                                        <div class="fields-group">
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label"> 品牌id</label>
                                                <div class="col-sm-8">
                                                    <div class="input-group input-group-sm">
                                                        <div class="input-group-addon">
                                                            <i class="fa fa-pencil"></i>
                                                        </div>

                                                        <input type="text" class="form-control brand_id" placeholder="品牌id" name="brand_id" value="@if(!empty($where['brand_id'])) {{$where['brand_id']}} @endif">
                                                    </div>    </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label"> 品牌名称</label>
                                                <div class="col-sm-8">
                                                    <div class="input-group input-group-sm">
                                                        <div class="input-group-addon">
                                                            <i class="fa fa-pencil"></i>
                                                        </div>

                                                        <input type="text" class="form-control goods_name" placeholder="品牌名称" name="brand_name" value="@if(!empty($where['brand_name'])) {{$where['brand_name']}} @endif">
                                                    </div>    </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label"> 是否官方授权</label>
                                                <div class="col-sm-8">
                                                    <select class="form-control is_official_auth" id="is_official_auth" name="is_official_auth" style="width: 100%;">
                                                        <option value="-1"></option>
                                                        <option value="1">否</option>
                                                        <option value="2">是</option>
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label"> 是否删除</label>
                                                <div class="col-sm-8">
                                                    <select class="form-control brand_is_del" id="brand_is_del" name="brand_is_del" style="width: 100%;">
                                                        <option value="-1"></option>
                                                        <option value="1">否</option>
                                                        <option value="2">是</option>
                                                    </select>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- /.box-body -->

                            <div class="box-footer">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="col-md-2"></div>
                                        <div class="col-md-8">
                                            <div class="btn-group pull-left">
                                                <button class="btn btn-info submit btn-sm"><i
                                                            class="fa fa-search"></i>&nbsp;&nbsp;搜索</button>
                                            </div>
                                            <div class="btn-group pull-left " style="margin-left: 10px;">
                                                <a href="/admin/brand/index" class="btn btn-default btn-sm"><i
                                                            class="fa fa-undo"></i>&nbsp;&nbsp;重置</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <!--筛选结束-->



                    <!-- /.box-header -->
                    <!--数据列表开始   -->
                    <div class="box-body table-responsive no-padding">
                        <table class="table table-hover">
                            <thead >
                            <tr >
                                <th>品牌id</th>
                                <th>品牌名称</th>
                                <th>品牌logo</th>
                                <th>注释</th>
                                <th>是否官方授权</th>
                                <th>是否删除</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>

                            <tbody>

                            @if (!empty($data['data']))
                                @foreach($data['data'] as $v)
                                    <tr >
                                        <td >
                                            {{ $v['brand_id'] }}
                                        </td>
                                        <td >
                                            {{$v['brand_name']}}
                                        </td>
                                        <td >
                                            <img src="{{$url}}{{$v['brand_logo']}}" width="100" />
                                        </td>
                                        <td >
                                            {{$v['brand_note']}}
                                        </td>
                                        <td >
                                            @if($v['is_official_auth']==1)
                                                <button data-type="is_official_auth" onclick="del(this,{{$v['brand_id']}})" style="color:red;">否</button>
                                            @else
                                                <button data-type="is_official_auth" onclick="del(this,{{$v['brand_id']}})" style="color:blue;">是</button>
                                            @endif
                                        </td>
                                        <td >
                                            @if($v['brand_is_del']==1)
                                                <button data-type="brand_is_del" onclick="del(this,{{$v['brand_id']}})" style="color:blue;">否</button>
                                            @else
                                                <button data-type="brand_is_del" onclick="del(this,{{$v['brand_id']}})" style="color:red;">是</button>
                                            @endif
                                        </td>
                                        <td >

                                            {{ $v['brand_creat_time'] }}

                                        </td>

                                        <td >
                                            <a href="/admin/brand/edit?brand_id={{$v['brand_id']}}">
                                                编辑
                                            </a> |

                                            <a class="grid-row-delete"  data-id="{{$v['brand_id']}}">
                                                删除
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            @endif
                            </tbody>
                        </table>
                    </div>
                    <!--数据列表结束-->



                    <!-- 分页开始    -->
                    <div class="box-footer clearfix">
                        @if($data['data'])
                            从 <b>{{$data['from']}}</b> 到 <b>{{$data['to']}}</b> ，总共 <b>{{$data['total']}}</b> 条
                            <ul class="pagination pagination-sm no-margin pull-right">
                                <!-- Previous Page Link -->
                                @if($data['page_list'])
                                    <li class="page-item @if($data['current_page']==1) disabled @endif ">
                                        @if($data['current_page']>1)
                                            <a class="page-link" data-href="{{$data['page_url']}}&page={{$data['up_page']}}" rel="next">&laquo;</a>
                                        @else
                                            <span class="page-link">&laquo;</span>
                                        @endif</li>
                                    @foreach($data['page_list'] as $v)
                                        @if($v=='...')
                                            <li class="page-item disabled"><span class="page-link">{{$v}}</span></li>
                                        @endif
                                        @if($v==$data['current_page'])
                                            <li class="page-item active"><span class="page-link">{{$v}}</span></li>
                                        @elseif($v!='...')
                                            <li class="page-item"><a class="page-link" data-href="{{$data['page_url']}}&page={{$v}}">{{$v}}</a></li>
                                        @endif
                                    @endforeach
                                    <li class="page-item @if($data['current_page']==$data['last_page']) disabled @endif ">
                                        @if($data['current_page']<$data['last_page'])
                                            <a class="page-link" data-href="{{$data['page_url']}}&page={{$data['next_page']}}" rel="next">&raquo;</a>
                                        @else
                                            <span class="page-link">&raquo;</span>
                                        @endif</li>
                                @endif
                            </ul>

                            <label class="control-label pull-right" style="margin-right: 10px; font-weight: 100;">
                                <small>显示</small>&nbsp;
                                <select class="input-sm grid-per-pager" name="per-page">
                                    <option value="{{$data['page_url']}}&per_page=10" >10</option>
                                    <option value="{{$data['page_url']}}&per_page=20" selected>20</option>
                                    <option value="{{$data['page_url']}}&per_page=30" >30</option>
                                    <option value="{{$data['page_url']}}&per_page=50" >50</option>
                                    <option value="{{$data['page_url']}}&per_page=100" >100</option>
                                </select>
                                &nbsp;<small>条</small>
                            </label>
                        @else
                            <div style="text-align: center;margin:20px;">未查到数据</div>
                        @endif
                    </div>
                    <!-- 分页结束    -->

                    <!-- /.box-body -->
                </div>
            </div>
        </div>

    </section>
</div>

<script data-exec-on-popstate>

    $(function () {
        $("#is_official_auth").val("@if(!empty($where['is_official_auth'])){{$where['is_official_auth']}}@endif");
        $("#brand_is_del").val("@if(!empty($where['brand_is_del'])){{$where['brand_is_del']}}@endif");
        //tr选中样式控制
        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
            if (this.checked) {
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $(this).closest('tr').css('background-color', '');
            }
        });

        //单条数据删除
        $('.grid-row-delete').unbind('click').click(function() {
            var id = $(this).data('id');
            console.log(id);
            swal({
                title: "确认删除?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        $.ajax({
                            method: 'POST',
                            url: '/admin/brand/delete',
                            data: {
                                _token:LA.token,
                                brand_id:id
                            },
                            dataType:'json',
                            success: function (data) {
                                if(data.status==1){
                                    swal(data.message, '', 'success');
                                    $.pjax.reload('#pjax-container');
                                }else{
                                    swal(data.message, '', 'error');
                                }
                                resolve(data);
                            }
                        });
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        swal(data.message, '', 'success');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        //刷新
        $('.grid-refresh').on('click', function() {
            $.pjax.reload('#pjax-container');
            toastr.success('刷新成功 !');
        });

        //搜索
        $('.shaixuan-filter-btn').unbind('click');
        $('.shaixuan-filter-btn').click(function (e) {
            if ($('#filter-box').is(':visible')) {
                $('#filter-box').addClass('hide');
            } else {
                $('#filter-box').removeClass('hide');
            }
        });
        @if($where)
        $('#filter-box').removeClass('hide');
        @endif

        //改变分页量
        $('.grid-per-pager').on("change", function(e) {
            $.pjax({url: this.value, container: '#pjax-container'});
        });

        //分页
        $("a.page-link").click(function(e){
            var url=$(this).attr('data-href');
            $.pjax({url:url, container: '#pjax-container'});
        })
    });

    //删除或不删除
    function del(e,id){
        var type=$(e).attr('data-type');
        $.ajax({
            url:"/admin/brand/ajaxFieldsEdit",
            data:{brand_id:id,_token:LA.token,type:type},
            type:'post',
            dataType:'json',
            success:function(m){
                if(m.status==1){
                    if(m.data.zhi==1){
                        $(e).html('否').css("color",m.data.color);
                    }else{
                        $(e).html('是').css("color",m.data.color);
                    }
                    toastr.success('ok');
                }else{
                    toastr.error(m.message);
                }
            }
        });
    }
</script>